package com.woniu.park.controller;


import com.woniu.park.controller.form.CommentForm;
import com.woniu.park.entity.Comment;
import com.woniu.park.entity.User;
import com.woniu.park.service.ICommentService;
import com.woniu.park.util.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author cxb
 * @since 2020-09-08
 */
@RestController
//@RequestMapping("/park/comment")
@Slf4j
public class CommentController {
    @Autowired
    private ICommentService commentService;
    /**
     * 发表评论
     * @return
     */
    @PostMapping("comment")
    public Result comment(@RequestBody @Valid CommentForm commentForm, HttpSession session){
        Comment comment = new Comment();
        //根据session查询当前发表评论的用户姓名
        User user1=(User) session.getAttribute("user");
        int userId=user1.getUserId();
        //测试从前端传入userId
//        User user=commentService.findUserById(commentForm.getCommentUserId());
        User user=commentService.findUserById(userId);
        BeanUtils.copyProperties(commentForm,comment);
        System.out.println("前"+comment.getCommentTag());
        if (commentForm.getCommentReplyUserId() != 0) {
            System.out.println("if");
            //根据commentForm.getCommentReplyUserId()查询回复方发表评论的用户姓名
            User replyUser=commentService.findUserById(commentForm.getCommentReplyUserId());
            comment.setCommentReplyUserName(replyUser.getUserName());
        }else{
            System.out.println("else");
            int maxTag=commentService.findMaxTag();
            System.out.println("maxTag:"+maxTag);
            comment.setCommentTag(maxTag+1);
            System.out.println("中"+comment.getCommentTag());
        }
            //如果没有评论等级默认5星好评
            if (commentForm.getCommentLevel() == 0) {
                comment.setCommentLevel(5);
            }
//        System.out.println("前"+comment.getCommentTag());
//        BeanUtils.copyProperties(commentForm,comment);
        comment.setCommentUserName(user.getUserName());
            comment.setCommentUserId(userId);
        System.out.println("后"+comment.getCommentTag());
        commentService.save(comment);
        List<Comment> comments=commentService.findCommentByParkspaceId(commentForm.getCommentParkspaceId());
        return Result.success("1","发表评论成功",comments);
    }

    /**
     * 获取历史评论
     * parkspaceId   车位id
     * @return
     */
    @GetMapping("getComment")
    public Result getComment(int parkspaceId){
        List<Comment> comments=commentService.findCommentByParkspaceId(parkspaceId);
        /*String[] text = new String[999];
        ArrayList<Object> objects = new ArrayList<>();
        for (Comment comment : comments) {
            objects.add(comment);
            objects.add(text);
        }*/

//        comments.add(text);
        return Result.success("1","查询历史评论成功",comments);
    }


}

